home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / lib.zoo / lib / term.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  12.0 KB  |  595 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: term.c,v 4.3 88/07/01 09:33:49 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/lib/RCS/term.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/lib/RCS/term.c,v $$Revision: 4.3 $";
  12.  
  13. /* routines for writing to mgr terminal emulator */
  14.  
  15. #include "term.h"
  16. #include "restart.h"
  17.  
  18. FILE    *m_termout;
  19. FILE    *m_termin;
  20. int    m_flags;
  21. int    m_envcount = 0;
  22. int    m_saveenvcount = 0;
  23. char    m_escchar = ESC;
  24. char    m_menuchar = M_DELIM;
  25.  
  26. jmp_buf _env;
  27.  
  28. struct sgttyb    sgtty__save[TTYMAX];
  29. int        sgtty_cnt = 0;
  30. char        m_linebuf[MAXLINE];
  31. static char    *m_fields[16];
  32.  
  33. /******************************************************************************
  34.  *
  35.  *    setup
  36.  */
  37.  
  38. int
  39. m_setup(flags)
  40. int flags;
  41.    {
  42.    m_flags = flags;
  43.  
  44.    if (!(m_flags&M_DEBUG)) {
  45.       m_termout = fopen(M_DEVICEOUT,"w");
  46.       m_termin = fopen(M_DEVICEIN,"r");
  47.       }
  48.  
  49.    if (m_termin == NULL || m_termout == NULL) 
  50.       m_flags |= M_DEBUG;
  51.  
  52.    if (m_flags&M_DEBUG) {
  53.       m_termin = stdin;
  54.       m_termout = stdout;
  55.       }
  56.    return(m_flags);
  57.    }
  58.  
  59. /******************************************************************************
  60.  *
  61.  *    get generic window parameters
  62.  */
  63.  
  64. int
  65. get_info(type,list)
  66. int type;
  67. char **list;
  68.    { 
  69.    if (type > G_MAX )
  70.       return(-1);
  71.    switch( type ) {
  72.    case G_ALL:
  73.    case G_ALLMINE:
  74.     return(-1);
  75.    }
  76.    _m_ttyset();
  77.    m_getinfo(type);
  78.    m_gets(m_linebuf);
  79.    _m_ttyreset();
  80.    return  parse(m_linebuf,list); 
  81.    }
  82.  
  83. /******************************************************************************
  84.  *
  85.  *    read window parameters off of standard input
  86.  */
  87.  
  88. int
  89. get_windata(windatap)
  90. struct window_data *windatap;
  91.    { 
  92.    if( parse(m_gets(m_linebuf),m_fields) < 8 )
  93.     return 0;
  94.    windatap->x = atoi(m_fields[0]);
  95.    windatap->y = atoi(m_fields[1]);
  96.    windatap->w = atoi(m_fields[2]);
  97.    windatap->h = atoi(m_fields[3]);
  98.    strcpy(windatap->tty,m_fields[4]);
  99.    windatap->num = atoi(m_fields[5]);
  100.    windatap->status = *m_fields[6];
  101.    windatap->setid = atoi(m_fields[7]);
  102.    return 1;
  103. }
  104.  
  105. /******************************************************************************
  106.  *
  107.  *    Get window parameters, one window at a time.
  108.  *    Returns 1 if window_data structure has been filled, 0 otherwise.
  109.  *    It is important to call get_eachwin() in a tight loop that doesn't
  110.  *    ever exit, so that all the data is picked up.
  111.  */
  112.  
  113. int
  114. get_eachwin( windatap )
  115. struct window_data *windatap;
  116.    { 
  117.    static int i = 0;
  118.  
  119.    if( !i ) {
  120.       _m_ttyset();
  121.       m_getinfo(G_ALL);
  122.    }
  123.    i = get_windata( windatap );
  124.    if( !i )
  125.       _m_ttyreset();
  126.    return(i);
  127.    }
  128.  
  129.  
  130. /******************************************************************************
  131.  *
  132.  *    Get window parameters for the current window set, one window at a time.
  133.  *    Returns 1 if window_data structure has been filled, 0 otherwise.
  134.  *    It is important to call get_eachcleintwin() in a tight loop that
  135.  *    doesn' tever exit, so that all the data is picked up.
  136.  */
  137.  
  138. int
  139. get_eachclientwin( windatap )
  140. struct window_data *windatap;
  141.    { 
  142.    static int i = 0;
  143.  
  144.    if( !i ) {
  145.       _m_ttyset();
  146.       m_getinfo(G_ALLMINE);
  147.    }
  148.    i = get_windata( windatap );
  149.    if( !i )
  150.       _m_ttyreset();
  151.    return(i);
  152.    }
  153.  
  154. /******************************************************************************
  155.  *
  156.  *    Get all window parameters.
  157.  *    NOTE CAREFULLY: The array of window_data structures pointed to by
  158.  *    list must be more than the total number of windows on the screen;
  159.  *    not a robust technique.
  160.  *    get_eachwin() is recommended above this.
  161.  */
  162.  
  163. int
  164. get_all(list)
  165. struct window_data *list;
  166.    { 
  167.    register int i;
  168.  
  169.    for(i=0;  get_eachwin( list );  i++ )
  170.       list++;
  171.    return(i);
  172.    }
  173.  
  174. /******************************************************************************
  175.  *
  176.  *    Get window parameters for client windows.
  177.  *    NOTE CAREFULLY: The array of window_data structures pointed to by
  178.  *    list must be more than the total number of windows on the screen;
  179.  *    not a robust technique.
  180.  *    get_eachclientwin() is recommended above this.
  181.  */
  182.  
  183. int
  184. get_client(list)
  185. struct window_data *list;
  186.    { 
  187.    register int i;
  188.  
  189.    _m_ttyset();
  190.    m_getinfo(G_ALLMINE);
  191.    for(i=0;  get_windata( list );  i++ )
  192.       list++;
  193.    _m_ttyreset();
  194.    return(i);
  195.    }
  196.  
  197. /******************************************************************************
  198.  *
  199.  *    get the window size
  200.  */
  201.  
  202. int
  203. get_size(x,y,wide,high)
  204. int *x, *y, *wide, *high;
  205.  
  206.    { 
  207.    register int count;
  208.  
  209.    if ((count = get_info(G_COORDS,m_fields)) >= 4) {
  210.       if (x)
  211.          *x = atoi(m_fields[0]); 
  212.       if (y)
  213.          *y = atoi(m_fields[1]); 
  214.       if (wide)
  215.          *wide = atoi(m_fields[2]); 
  216.       if (high)
  217.          *high = atoi(m_fields[3]); 
  218.       return(count);
  219.       }
  220.    else return(-count);
  221.    }
  222.  
  223. /******************************************************************************
  224.  *
  225.  *    get the mouse coords
  226.  */
  227.  
  228. int
  229. get_mouse(x,y)
  230. int *x, *y;
  231.  
  232.    { 
  233.    register int count;
  234.  
  235.    if ((count = get_info(G_MOUSE2,m_fields)) >= 3) {
  236.       if (x)
  237.          *x = atoi(m_fields[0]); 
  238.       if (y)
  239.          *y = atoi(m_fields[1]); 
  240.       return(atoi(m_fields[2]));
  241.       }
  242.    else return(-count);
  243.    }
  244.  
  245. /******************************************************************************
  246.  *
  247.  *    get system parameters
  248.  */
  249.  
  250. int
  251. get_param(host,xmax,ymax,border)
  252. char *host;
  253. int *xmax, *ymax, *border;
  254.  
  255.    { 
  256.    register int count;
  257.  
  258.    if ((count = get_info(G_SYSTEM,m_fields)) >= 4) {
  259.       if (host)
  260.          strcpy(host,m_fields[0]);
  261.       if (xmax)
  262.          *xmax = atoi(m_fields[1]); 
  263.       if (ymax)
  264.          *ymax = atoi(m_fields[2]); 
  265.       if (border)
  266.          *border = atoi(m_fields[3]); 
  267.       return(count);
  268.       }
  269.    else return(-count);
  270.    }
  271.  
  272. /******************************************************************************
  273.  *
  274.  *    get the cursor position
  275.  */
  276.  
  277. int
  278. get_cursor(x,y)
  279. int *x, *y;
  280.  
  281.    { 
  282.    register int count;
  283.  
  284.    if ((count = get_info(G_CURSOR,m_fields)) > 2) {
  285.       if (x)
  286.          *x = atoi(m_fields[0]); 
  287.       if (y)
  288.          *y = atoi(m_fields[1]); 
  289.       return(2);
  290.       }
  291.    else return(-count);
  292.    }
  293.  
  294. /******************************************************************************
  295.  *
  296.  *    get the window size - rows and columns
  297.  */
  298.  
  299. int
  300. get_colrow(cols,rows)
  301. int *cols, *rows;
  302.  
  303.    { 
  304.    register int count;
  305.  
  306.    if ((count = get_info(G_WINSIZE,m_fields)) == 2) {
  307.       if (cols)
  308.          *cols = atoi(m_fields[0]); 
  309.       if (rows)
  310.          *rows = atoi(m_fields[1]); 
  311.       return(2);
  312.       }
  313.    else return(-count);
  314.    }
  315.  
  316. /******************************************************************************
  317.  *
  318.  *    get the termcap entry
  319.  */
  320.  
  321. char *
  322. get_termcap()
  323.    { 
  324.    _m_ttyset();
  325.    m_getinfo(G_TERMCAP);
  326.    m_gets(m_linebuf);
  327.    _m_ttyreset();
  328.    return(m_linebuf);
  329.    }
  330.  
  331. /******************************************************************************
  332.  *
  333.  *    get the font size
  334.  */
  335.  
  336. int
  337. get_font(wide,high)
  338. int  *wide, *high;
  339.  
  340.    { 
  341.    register int count, result;
  342.  
  343.    if ((count = get_info(G_FONT,m_fields)) >= 3) {
  344.       if (wide)
  345.          *wide = atoi(m_fields[0]); 
  346.       if (high)
  347.          *high = atoi(m_fields[1]); 
  348.       result = atoi(m_fields[2]); 
  349.       return(result);
  350.       }
  351.    else return(-count);
  352.    }
  353.  
  354. /******************************************************************************
  355.  *
  356.  *    make a new window
  357.  */
  358.  
  359. int
  360. m_makewindow(x,y,wide,high)
  361. int  x,y,wide,high;
  362.    { 
  363.    register int count, result;
  364.    _m_ttyset();
  365.    m_newwin(x,y,wide,high);
  366.    m_gets(m_linebuf);
  367.    _m_ttyreset();
  368.    return(atoi(m_linebuf));
  369.    }
  370.  
  371. /******************************************************************************
  372.  *
  373.  *    see if window is active
  374.  */
  375.  
  376. int
  377. is_active()
  378.    { 
  379.    *m_linebuf = '\0';
  380.    get_info(G_STATUS,m_fields);
  381.    return(*m_linebuf == 'a');
  382.    }
  383.  
  384. /******************************************************************************
  385.  *
  386.  *    return last line read
  387.  */
  388.  
  389. char *
  390. m_lastline()
  391.    {
  392.    return(m_linebuf);
  393.    }   
  394.  
  395. /******************************************************************************
  396.  *
  397.  *    down load a menu
  398.  */
  399.  
  400. menu_load(n,count,text)
  401. int n;                /* menu number */
  402. int count;            /* number of menu items */
  403. struct menu_entry *text;    /* menu choices */
  404.    {
  405.    register int i, len;
  406.  
  407.    if (text == (struct menu_entry *) 0)
  408.       return (-1);
  409.  
  410.    /* calculate string lengths */
  411.  
  412.    len = 2 * count + 1;
  413.  
  414.    for (i=0;i<count;i++)
  415.        len += strlen(text[i].value) + strlen(text[i].action);
  416.    
  417.    fprintf(m_termout,"%c%d,%d%c%c",m_escchar,n,len,E_MENU,m_menuchar);
  418.  
  419.    for (i=0;i<count;i++)
  420.       fprintf(m_termout,"%s%c",text[i].value,m_menuchar);
  421.  
  422.    for (i=0;i<count;i++)
  423.       fprintf(m_termout,"%s%c",text[i].action,m_menuchar);
  424.  
  425.    m_flush();
  426.    }
  427.  
  428. /******************************************************************************
  429.  *
  430.  *    download a bitmap 
  431.  */
  432.  
  433. m_bitload(x,y,w,h,data)
  434. int x,y;
  435. int w,h;
  436. register char *data;
  437.    {
  438.    register int size = h * ((w+15)&~15)/8;        /* round to 16 bit boundary */
  439.    m_bitld(w,h,x,y,size);
  440.    while(size-- > 0)
  441.       fputc(*data++,m_termout);
  442.    m_flush();
  443.    }
  444.  
  445. /******************************************************************************
  446.  *
  447.  *    Set and save the terminal mode  (if required);
  448.  */
  449.  
  450. m_ttyset()
  451.    {
  452.     int code;
  453.    struct sgttyb buff;
  454.  
  455.    code = gtty(fileno(m_termout),sgtty__save + sgtty_cnt);
  456.  
  457.    if (sgtty__save[sgtty_cnt].sg_flags&(ECHO|RAW)) {
  458.       buff = sgtty__save[sgtty_cnt];
  459.       buff.sg_flags &= ~(ECHO|RAW);
  460.       m_flush();
  461.       stty(fileno(m_termout),&buff);
  462.       }
  463.  
  464.    if (sgtty_cnt < TTYMAX)
  465.       sgtty_cnt++;
  466.  
  467.     return(code);
  468.    }
  469.  
  470.  
  471. /******************************************************************************
  472.  *
  473.  *    Restore the terminal mode 
  474.  */
  475.  
  476. m_ttyreset()
  477.    {
  478.    if (sgtty_cnt)
  479.       sgtty_cnt--;
  480.    else
  481.       return(1);
  482.  
  483.    if (sgtty__save[sgtty_cnt].sg_flags&(ECHO|RAW)) {
  484.       m_flush();
  485.       return(stty(fileno(m_termout),sgtty__save + sgtty_cnt));
  486.       }
  487.    else
  488.       return(0);
  489.    }
  490.  
  491. /******************************************************************************
  492.  *
  493.  *    change the terminal modes
  494.  */
  495.  
  496. m_resetflags(flags)
  497.    {
  498.    struct sgttyb buff;
  499.       gtty(fileno(m_termin),&buff);
  500.       if (buff.sg_flags & flags) {
  501.          buff.sg_flags &= ~flags;
  502.      m_flush();
  503.          stty(fileno(m_termin),&buff);
  504.          }
  505.    }
  506.  
  507. m_setflags(flags)
  508.    {
  509.    struct sgttyb buff;
  510.       gtty(fileno(m_termin),&buff);
  511.       if (!( buff.sg_flags & flags)) {
  512.          buff.sg_flags |= flags;
  513.      m_flush();
  514.          stty(fileno(m_termin),&buff);
  515.          }
  516.    }
  517.  
  518. /**
  519.     Given a bitmap id and an icon name,
  520.     have mgr load that icon into that bitmap, returning the icon width
  521.     and height via the given integer pointers.
  522.     Return a positive number if successful.
  523.     If the icon is not loaded, set the width and height values to 0 and
  524.     return 0.
  525. */
  526. int
  527. m_bitfile( bitmapid, iconname, iconwidthp, iconheightp )
  528. int    bitmapid;
  529. char    *iconname;
  530. int    *iconwidthp,
  531.     *iconheightp;
  532.    {
  533.     *iconwidthp = *iconheightp = 0;
  534.     m_bitfromfile( bitmapid, iconname );
  535.     m_flush();
  536.     return( sscanf( m_get(), "%d %d", iconwidthp, iconheightp ) == 2 );
  537.    }
  538.  
  539. /*****************************************************************************
  540.  *    parse a line into fields
  541.  */
  542.  
  543. #ifndef iswhite
  544. #define iswhite(x)    ((x)==' ' || (x)=='\t')
  545. #endif
  546.  
  547. int
  548. parse(line,fields)
  549. register char *line;
  550. register char **fields;
  551.    {
  552.    int inword = 0;
  553.    int count = 0;
  554.    char *start;
  555.    register char c;
  556.  
  557.    for(start = line;(c = *line) && c != '\n';line++)
  558.       if (inword && iswhite(c)) {
  559.          inword = 0;
  560.          *line = '\0';
  561.          *fields++ = start;
  562.          count++;
  563.          }
  564.       else if (!inword && !iswhite(c)) {
  565.          start = line;
  566.          inword = 1;
  567.          }
  568.  
  569.    if (inword) {
  570.       *fields++ = start;
  571.       count++;
  572.       if (c == '\n')
  573.          *line = '\0';
  574.       }
  575.    *fields = (char *) 0;
  576.    return(count);
  577.    }
  578.  
  579. /******************************************
  580.  *    stuff for restarting
  581.  */
  582.  
  583. _Catch()
  584.    {
  585.    ioctl(fileno(m_termin),TIOCFLUSH,0);
  586.    longjmp(_env,1);
  587.    }
  588.  
  589. _Clean()
  590.    {
  591.    while(m_saveenvcount < m_envcount)
  592.       m_pop();
  593.    exit(1);
  594.    }
  595.